#!/usr/bin/python
# -*-coding:utf-8-*-
import numpy as np
import pandas as pd

def selectX(X_candidates, selected_number=None):
    if selected_number is None:
        selected_number = len(X_candidates) // 2

    if type(X_candidates) == list:
        assign_prob = 1. / len(X_candidates)

        X_candidates_dict = {
           f: assign_prob for f in X_candidates
        }
    else:
        X_candidates_dict = X_candidates

    X_candidates_df = pd.DataFrame({'prob':list(X_candidates_dict.values())},
                                            index=list(X_candidates_dict.keys()))

    X_candidates_df['prob'] = X_candidates_df['prob'].cumsum()

    selected_X_list = []
    count = 0
    while True:
        X_candidates_df['select_prob'] = np.random.uniform(0, 1)

        selected_prob = X_candidates_df['prob'] - X_candidates_df['select_prob']

        selected = selected_prob[selected_prob >= 0].index[0]

        if selected in selected_X_list:
            continue

        selected_X_list.append(selected)

        count += 1

        if count >= selected_number:
            break

    return selected_X_list



